package com.yanqu.road.logic.activity.zhenhuan;

import com.yanqu.road.dao.impl.activity.zhenhuan.ZhenHuanImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.zhenhuan.config.ZhenHuanConcubineConfig;
import com.yanqu.road.entity.activity.zhenhuan.config.ZhenHuanStoryConfig;
import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.logic.helper.WeightHelper;
import com.yanqu.road.pb.activity.ZhenHuanProto;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZhenHuanFirstProcessConfig {
    private ActivityInfo activityInfo;
    private Map<Integer, ZhenHuanStoryConfig> storyConfigMap = new HashMap<>(); // 故事配置
    private List<NormalWeightItem> storyWeightList = new ArrayList<>(); // 故事权重
    private Map<Integer, ZhenHuanConcubineConfig> concubineConfigMap = new HashMap<>(); // 妃子配置
    private int choseCost;
    private int powerGoodsId; // 恢复体力道具ID
    private int exchangeGoodsId; // 兑换物品ID
    private int initPower; // 初始体力
    private long powerRecoverCD; //恢复CD秒
    private int concubineMaxNum; // 妃子最大数量
    private int promotionNum; //晋升数量
    private List<List<Integer>> unlockAmountWeightList = new ArrayList<>(); //解锁故事数对应的触发概率，千分比
    private int guaranteeNum;
    private RandomHelper randomHelper = new RandomHelper();

    private Map<String, ZhenHuanProto.ZhenHuanFirstProcessConfigSyncMsg> configMsgMap = new HashMap<>();

    public ZhenHuanFirstProcessConfig(ActivityInfo activityInfo, Map<String, ActivityConfig> activityConfigMap) {
        this.activityInfo = activityInfo;

        this.storyConfigMap = new ZhenHuanImpl().getZhenHuanStoryConfigMap(activityInfo.getActivityId());
        this.concubineConfigMap = new ZhenHuanImpl().getZhenHuanConcubineConfigMap(activityInfo.getActivityId());

        this.powerGoodsId = activityConfigMap.get("ZHENHUAN_1_ENERGY_ITEM_ID").getIntValue();
        this.choseCost = activityConfigMap.get("ZHENHUAN_1_ENERGY_CONSUME").getIntValue();
        this.exchangeGoodsId = activityConfigMap.get("EXCHANGE_SCORE_ITEM_ID").getIntValue();
        this.initPower = activityConfigMap.get("ZHENHUAN_1_INITIAL_ENERGY").getIntValue();
        this.powerRecoverCD = activityConfigMap.get("ZHENHUAN_1_ENERGY_RECOVER").getLongValue();
        this.promotionNum = activityConfigMap.get("ZHENHUAN_1_QUALIFICATION").getIntValue();
        List<Integer> list = StringUtils.stringToIntegerList(activityConfigMap.get("ZHENHUAN_1_LENGTH_AND_WIDTH").getValue(), "\\|");
        this.concubineMaxNum = list.get(0) * list.get(1);

        for (ZhenHuanStoryConfig storyConfig : this.storyConfigMap.values()) {
            storyWeightList.add(new NormalWeightItem(BigInteger.valueOf(storyConfig.getId()), storyConfig.getWeight()));
        }

        if (activityConfigMap.containsKey("ZHENHUAN_1_DRAW_ALL")) {
            List<List<Integer>> tempList = new ArrayList<>();
            String[] split = activityConfigMap.get("ZHENHUAN_1_DRAW_ALL").getValue().split("\\|");
            for (String s : split) {
                List<Integer> attrs = StringUtils.stringToIntegerList(s, ";");
                tempList.add(attrs);
            }
            this.unlockAmountWeightList = tempList;
        }

        if (activityConfigMap.containsKey("ZHENHUAN_1_DRAW_GUARANTEES")) {
            this.guaranteeNum = activityConfigMap.get("ZHENHUAN_1_DRAW_GUARANTEES").getIntValue();
        }
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public int getActivityId() {
        return activityInfo.getActivityId();
    }

    public Map<Integer, ZhenHuanStoryConfig> getStoryConfigMap() {
        return storyConfigMap;
    }

    public Map<Integer, ZhenHuanConcubineConfig> getConcubineConfigMap() {
        return concubineConfigMap;
    }

    public int getChoseCost() {
        return choseCost;
    }

    public int getPowerGoodsId() {
        return powerGoodsId;
    }

    public int getExchangeGoodsId() {
        return exchangeGoodsId;
    }

    public int getInitPower() {
        return initPower;
    }

    public long getPowerRecoverCD() {
        return powerRecoverCD;
    }


    public int getConcubineMaxNum() {
        return concubineMaxNum;
    }

    public ZhenHuanProto.ZhenHuanFirstProcessConfigSyncMsg getConfigMsg(String language) {
        return configMsgMap.get(language);
    }

    public void setConfigMsg(String language, ZhenHuanProto.ZhenHuanFirstProcessConfigSyncMsg configMsg) {
        configMsgMap.put(language, configMsg);
    }

    public int randomStoryId() {
        NormalWeightItem randomResult = WeightHelper.getRandomResult(storyWeightList);
        if (randomResult == null) {
            return 0;
        }
        return randomResult.getValue().intValue();
    }

    public ZhenHuanStoryConfig getStoryConfig(int storyId) {
        return storyConfigMap.get(storyId);
    }

    public ZhenHuanConcubineConfig getConcubineConfig(int concubineId) {
        return concubineConfigMap.get(concubineId);
    }

    public int getPromotionNum() {
        return promotionNum;
    }

    public int getUnlockAmountWeight(int unlockAmount) {
        int weight = 0;
        for (List<Integer> list : unlockAmountWeightList) {
            Integer startNum = list.get(0);
            if (unlockAmount >= startNum) {
                weight = list.get(1);
            } else {
                break;
            }
        }
        return weight;
    }

    public boolean randomSpecialEffect(int unlockNAmount) {
        int weight = getUnlockAmountWeight(unlockNAmount);
        if (weight == 0) {
            return false;
        }
        int randomVale = randomHelper.next(1000);
        return randomVale < weight;
    }

    public List<Integer> randomPosList(int num, int excludePos) {
        List<Integer> randomPostList = randomHelper.getRandomDistinctInteger(1, concubineMaxNum, num);
        if (randomPostList.contains(excludePos)) {
            randomPostList.remove(Integer.valueOf(excludePos));
        }
        return randomPostList;
    }

    public int getGuaranteeNum() {
        return guaranteeNum;
    }
}
